using System;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Text;

namespace QuickUIT.Controls
{
    /// <summary>
    /// Gets an edit type control, such as a textbox. 
    /// </summary>
    public class UIEdit : UIBaseControl
    {
        /// <summary>
        /// Gets whether the current edit control is a password-type control.
        /// </summary>
        public bool IsPassword
        {
            get { return Control.Current.IsPassword; }
        }

        /// <summary>
        /// Specifies the range value of the edit control, if it takes a numeric range. 
        /// If the control does not support the <see cref="RangeValuePattern"/>, returns NaN. 
        /// </summary>
        public new double RangeValue
        {
            get { return base.RangeValue; }
            set { SetRangeValue(value);}
        }

        /// <summary>
        /// Gets the current value of the edit control. 
        /// </summary>
        public new string Value
        {
            get { return base.Value; }
            set { SetValue(value); }
        }

        /// <summary>
        /// Gets whether the current control is in the read-only state. 
        /// </summary>
        public new bool IsReadOnly
        {
            get { return base.IsReadOnly; }
        }

        /// <summary>
        /// Gets a value which indicates whether the range value of a control is read-only. 
        /// </summary>
        public new bool IsRangeReadOnly
        {
            get { return base.IsRangeReadOnly; }
        }

        /// <summary>
        /// Gets the smallest value to which the contents of the edit control can be set. 
        /// </summary>
        public new double Minimum
        {
            get { return base.Minimum; }
        }

        /// <summary>
        /// Gets the largest value to which the contents of the edit control can be set. 
        /// </summary>
        public new double Maximum
        {
            get { return base.Maximum; }
        }

        /// <summary>
        /// Gets a value which indicate the number of decimal places that the value of edit control can be set to. 
        /// </summary>
        public new double SmallChange
        {
            get { return base.SmallChange; }
        }

        /// <summary>
        /// Gets the text range that encloses the main text of a document. 
        /// </summary>
        public new TextPatternRange DocumentRange
        {
            get { return base.DocumentRange; }
        }

        /// <summary>
        /// Gets a value that indicates the type of text selection that is supported by the control. 
        /// </summary>
        public new SupportedTextSelection SupportedTextSelection
        {
            get { return base.SupportedTextSelection; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIEdit"/> class. 
        /// </summary>
        public UIEdit() : base(ControlType.Edit)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIEdit"/> class.
        /// </summary>
        /// <param name="parent">The parent of this control.</param>
        public UIEdit(UIBaseControl parent) : base(parent, ControlType.Edit)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIEdit"/> class.
        /// </summary>
        /// <param name="locator">The locator used to find the specific control.</param>
        public UIEdit(string locator) : base(null, ControlType.Edit, locator)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIEdit"/> class.
        /// </summary>
        /// <param name="parent">The parent of this control.</param>
        /// <param name="locator">The locator used to find the specific control.</param>
        public UIEdit(UIBaseControl parent, string locator) : base(parent, ControlType.Edit, locator)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIEdit"/> class.
        /// </summary>
        /// <param name="control">The underlying <see cref="AutomationElement"/> of the control.</param>
        public UIEdit(AutomationElement control) : base(control, ControlType.Edit)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIEdit"/> class.
        /// </summary>
        /// <param name="parent">The parent of the control</param>
        /// <param name="control">The underlying <see cref="AutomationElement"/> of the control.</param>
        public UIEdit(UIBaseControl parent, AutomationElement control) : base(parent, control, ControlType.Edit)
        {
        }

        /// <summary>
        /// Retrieves a collection of text ranges that represents the currently selected text in a text-based control.
        /// </summary>
        /// <returns>A collection of disjoint text ranges.</returns>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public new TextPatternRange[] GetTextSelection()
        {
            return base.GetTextSelection(); 
        }

        /// <summary>
        /// Retrieves an array of disjoint text ranges from a text-based control where each text range represents a contiguous span of visible text.
        /// </summary>
        /// <returns>The collection of visible text ranges within the container or an empty array.</returns>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public new TextPatternRange[] GetVisibleRanges()
        {
            return base.GetVisibleRanges();
        }

        /// <summary>
        /// Retrieves a text range enclosing a child element such as an image, hyperlink, Microsoft Excel spreadsheet, or other embedded object. 
        /// </summary>
        /// <returns>A range that spans the child element.</returns>
        /// <exception cref="ArgumentNullException">The child element is null.</exception>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public new TextPatternRange RangeFromChild(AutomationElement childElement)
        {
            return base.RangeFromChild(childElement); 
        }

        /// <summary>
        /// Returns the degenerate (empty) text range nearest to the specified screen coordinates. 
        /// </summary>
        /// <param name="screenLocation">The location in screen coordinates.</param>
        /// <returns>A degenerate range nearest the specified location. Null is never returned.</returns>
        /// <exception cref="ArgumentException">The given point is outside the <see cref="AutomationElement"/> associated with the text pattern.</exception>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public new TextPatternRange RangeFromPoint(Point screenLocation)
        {
            return base.RangeFromPoint(screenLocation);
        }

        /// <summary>
        /// Clears the contents of the edit control. 
        /// </summary>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public void Clear()
        {
            SetValue(String.Empty);
        }
    }
}
